package alipay

import (
	"context"
	"encoding/json"
	"fmt"

	"gitee.com/wosylf/ltool"
)

func (a *Client) F订单同步(ctx context.Context, bm ltool.BodyMap) (aliRsp *MerchantOrderSyncRsp, err error) {
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.merchant.order.sync"); err != nil {
		return nil, err
	}
	aliRsp = new(MerchantOrderSyncRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}
